package com.ego.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ego.common.pojo.CodecUtils;
import com.ego.common.pojo.NumberUtils;
import com.ego.exception.enums.ExceptionEnum;
import com.ego.exception.exception.EgoException;
import com.ego.user.mapper.UserMapper;
import com.ego.user.pojo.User;
import com.ego.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private AmqpTemplate amqpTemplate;


    /**
     * @param data 需要校验的数据
     * @param type 1:校验用户名 2：校验手机号
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Boolean check(String data, Integer type) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (type == 1){
            queryWrapper.eq("username",data);
        }{
            queryWrapper.eq("phone",data);
        }
        Integer count = userMapper.selectCount(queryWrapper);
        return count==0;
    }



    @Override
    public void send(String phone) {
        try {
            //1.生成6位随机验证码
            String code = NumberUtils.generateCode(6);
            //2.存储到redis
            stringRedisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
            //3.异步发送信息到mq
            amqpTemplate.convertAndSend("ego.sms.exchange", "sms.verify.code", phone + "&&&" + code);
        } catch (Exception e) {
            EgoException.error(log,ExceptionEnum.SMS_SEND_ERROR);
        }
    }

    @Override
    public void register(User user, String code) {
        //1.校验验证码
        String redisCode = stringRedisTemplate.opsForValue().get(user.getPhone());
        if (!code.equals(redisCode)) {
            EgoException.error(log,ExceptionEnum.VERIFY_CODE_NOT_MATCHING);
        }
        //2.密码加密
        String password = CodecUtils.passwordBcryptEncode(user.getUsername(), user.getPassword());
        user.setPassword(password);
        //3.保存数据库
        user.setCreated(new Date());
        userMapper.insert(user);
    }

    @Override
    @Transactional(readOnly = true)
    public User queryUser(String username, String password) {
        // 查询
        User record = new User();
        record.setUsername(username);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",record.getUsername());
        User user = userMapper.selectOne(queryWrapper);
        // 校验用户名
        if (user == null) {
            return null;
        }
        // 校验密码
        if (!CodecUtils.passwordConfirm(username+password,user.getPassword())) {
            return null;
        }
        // 用户名密码都正确
        return user;
    }


}
